<template>
  <div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      v-loading="loading"
      max-height="650"
    >
      <el-table-column prop="title" label="封面图" width="80">
        <template slot-scope="{ row }">
          <img
            class="cover-img"
            :src="row.fileId ? row.sysAttachment.httpUrl : defaultCourseImg"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="课程标题">
        <template slot-scope="{ row }">
          <el-link @click="handleManage(row)" :underline="false">{{
            row.title
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="sourceUnit" label="单位">
        <template slot-scope="scope">
          <span>{{ showUnit(scope.row.tbSysDept) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="subhead" label="课程团队">
        <template slot-scope="scope">
          <span>{{
            scope.row.sysCourseTeamList.map((v) => v.userName).toString()
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="recommended" label="推荐状态">
        <template slot-scope="scope">
          <span v-show="scope.row.recommended == 0">未推荐</span>
          <span class="color-success" v-show="scope.row.recommended == 1"
            >已推荐</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审核状态">
        <template slot-scope="scope">
          <span v-show="scope.row.auditStatus == 1">待审核</span>
          <span class="color-success" v-show="scope.row.auditStatus == 2"
            >审核通过</span
          >
          <span class="color-danger" v-show="scope.row.auditStatus == 3"
            >不通过</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="resultDes" label="审核备注">
        <template slot-scope="scope">
          <span>{{ scope.row.resultDes || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              v-if="listQuery.auditStatus == 1"
              type="custom"
              size="mini"
              @click="handleAudit(row)"
            >
              审核
            </el-button>
            <el-button
              v-if="
                listQuery.auditStatus != 1 &&
                row.recommended != 1 &&
                row.auditStatus == 2
              "
              type="primary"
              size="mini"
              @click="handleRecommend(row, 1)"
            >
              推荐
            </el-button>
            <el-button
              v-if="
                listQuery.auditStatus != 1 &&
                row.recommended == 1 &&
                row.auditStatus == 2
              "
              type="warning"
              size="mini"
              @click="handleRecommend(row, 0)"
            >
              取消推荐
            </el-button>
            <el-button
              type="success"
              size="mini"
              :disabled="row.createBy == $store.getters.userId"
              @click="handleMessage(row)"
            >
              私信
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
    <SendMessage />
  </div>
</template>

<script>
import {
  sysCourseQuery,
  sysCourseUpdate,
} from "@/api/admin/courseLibrary/sysCourse.js";
import Bus from "@/utils/bus.js";
import webinfo from "@/mixins/webinfo.js";
export default {
  mixins: [webinfo],
  components: {
    SendMessage: () => import("@/components/sendMessage"),
  },
  data() {
    return {
      loading: false,
      listQuery: {
        page: 1,
        limit: 10,
        courseType: this.$route.meta.type,
        status: "1", //课程状态：0-未发布，1-已发布
        auditStatus: "1",
      },
      tableData: [],
      pageObj: {},
    };
  },
  props: ["type"],
  computed: {
    showUnit: function () {
      return function (data) {
        if (data && data.childList) {
          return data.childList[0].name;
        } else {
          return "--";
        }
      };
    },
  },
  watch: {
    type(v) {
      this.listQuery.courseType = v;
      this.listQuery.auditStatus = 1;
      this.getList();
    },
  },
  mounted() {
    this.listQuery.courseType = this.type;
    this.getList();
    Bus.$on("refreshCourseList", (v) => {
      if (v) {
        Object.assign(this.listQuery, v);
      }
      this.getList();
    });
  },
  beforeDestroy() {
    Bus.$off("refreshCourseList");
  },
  methods: {
    getList() {
      this.loading = true;
      sysCourseQuery(this.listQuery).then((res) => {
        setTimeout(() => {
          this.tableData = res.data.data;
          this.pageObj = res.data;
          this.loading = false;
        }, 200);
      });
    },
    //审核
    handleAudit(row) {
      Bus.$emit("showDialogCourseAudit", row);
    },
    handleRecommend(row, type) {
      sysCourseUpdate({ id: row.id, recommended: type }).then((res) => {
        this.getList();
      });
    },
    // 发送私信
    handleMessage(row) {
      Bus.$emit("sendMessage", [row.createBy]);
    },
  },
};
</script>
<style scoped>
.cover-img {
  display: block;
  width: 40px;
  height: 30px;
}
</style>